package com.example.langchain4j.service;

import dev.langchain4j.agent.tool.Tool;
import dev.langchain4j.model.output.structured.Description;
import lombok.Data;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class NewsService {

    private static final String BAIDU_NEWS_URL = "https://news.baidu.com/";

    @Tool("今天的热点新闻")
    public List<News> getHotNews() throws IOException {
        List<News> newsList = new ArrayList<>();

        Document doc = Jsoup.connect(BAIDU_NEWS_URL).get();
        Elements newsElements = doc.select(".hotnews ul li a");

        for (Element newsElement : newsElements) {
            String title = newsElement.text();
            News news = new News();
            news.setTitle(title);
            newsList.add(news);
        }

        return newsList;
    }

    @Data
    public static class News {

        @Description("新闻标题")
        String title;
    }

}
